﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Unity;

namespace Skynet.C2SModule.Views.BookWritter
{
    /// <summary>
    /// Interaction logic for BookWritterView.xaml
    /// </summary>
    public partial class BookWritterView : UserControl, IBookWritterView
    {
        private Presenters.BookWritterPresenter _Presenter;
        [Dependency]
        public Presenters.BookWritterPresenter Presenter
        {
            get { return _Presenter; }
            set { _Presenter = value;
            this._Presenter.View = this;
            this.DataContext = value;
            }
        }
        

        public BookWritterView()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.Presenter.OnViewReady();
        }

        public string BookContent
        {
            get
            {
                return this.txtEditor.Document;
            }
            set
            {
                this.txtEditor.Document = value;
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog printDialog = new PrintDialog();
            if (printDialog.ShowDialog() == true)
            {
                var doc = this.txtEditor.FlowDocument;

                double pageHeight = doc.PageHeight;
                double pageWidth = doc.PageWidth;
                Thickness pagePadding = doc.PagePadding;
                double columnGap = doc.ColumnGap;
                double columnWidth = doc.ColumnWidth;

                doc.PageHeight = printDialog.PrintableAreaHeight;
                doc.PageWidth = printDialog.PrintableAreaWidth;
                doc.PagePadding = new Thickness(50);
                doc.ColumnWidth = (doc.PageWidth - doc.ColumnGap - doc.PagePadding.Left - doc.PagePadding.Right) / 2;
                printDialog.PrintDocument( ((IDocumentPaginatorSource)doc).DocumentPaginator, "A Flow Document");
                doc.PageHeight = pageHeight;
                doc.PageWidth = pageWidth;
                doc.PagePadding = pagePadding;
                doc.ColumnGap = columnGap;
                doc.ColumnWidth = columnWidth;
            }
        }
    }
}
